<?php 
		namespace Admin\Model;
		use Think\Model;
		class SearchModel extends Model	{
			protected $tableName = 'search';
			
			
			/**
			 * 获取模型
			 */
			public function getModel(){
				$sql = " SELECT
						think_model.id,
						think_model.`name`,
						think_model.`table`,
						think_model.description,
						think_model.`show`,
						think_search.model_id
						FROM
						think_search
						RIGHT JOIN think_model ON think_search.model_id = think_model.id
						GROUP BY
						think_model.id
			 ";
				return $this->query($sql); 
			}
			
			/**
			 *获取模型列表 
			 */
			public function getList(){
				$sql = "SELECT
						think_model.id,
						think_model.`name`,
						think_model.`table`,
						think_model.description,
						think_model.`show`,
						think_search.model_id
						FROM
						think_search
						left JOIN think_model ON think_search.model_id = think_model.id
						GROUP BY
						think_search.model_id
			";
				return $this->query($sql);
			}
			
			/**
			 * 保持模型
			 */
			public function saveData($data){
				$sql = 'TRUNCATE think_search';
				$this->query($sql);
				foreach ($data as $r){
					$result = $this->add(array(
							'model_id'=>$r,
							));
					}
				return $result;
			}
			
			
			/**
			 * 更新 关键字
			 */
			
			public function upKey(){
				$model = $this->getList();
				$sql = "";
				foreach ($model as $r ){
					$sql = $sql."SELECT id as data_id,cate_id,title,description FROM think_".$r['table']." UNION ALL ";
				}
				$sql = substr($sql,0, strlen($sql)-10);
				$list = $this->query($sql);
				
				$db = D('search_data');
				$sql = 'TRUNCATE think_search_data';
				$db->query($sql);
				
				foreach ($list as $r){
					$result = $db->add($r);
				}
				return $result;
			}
			
			/**
			 * 关键字搜索
			 */
			public function searchData($key){
				
				$sql = "SELECT * FROM think_search_data WHERE title LIKE '%$key%' OR description LIKE '%$key%' ORDER BY data_id DESC";
				$list = $this->query($sql);
				return  $list;
			}
			
		}
		


?>